﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreesenseSubAIS.Core
{
    public class LocalGenerator : IImageGenerator
    {
        public event EventHandler<Bitmap> ImageReady;
        private string _folderpath;
        private List<string> _filenamelist;
        public LocalGenerator(string folderpath)
        {
            _folderpath = folderpath;
        }
        public async Task Init()
        {
            bool flag = false;
            try
            {
                _filenamelist = GetExistProduct();
                if (_filenamelist != null && _filenamelist.Count > 0)
                {
                    flag = true;
                }
                await Task.Delay(10);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("本地图片生成器初始化失败！" + ex.ToString());
            }
        }
        public bool AquireBitmap()
        {
            bool flag = false;
            try
            {
                if (ImageReady != null)
                    ImageReady("LocalGenerator",GetmapByIndex(0));
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        public bool AquireBitmap(string mapname)
        {
            bool flag = false;
            try
            {
                if (_filenamelist.Contains(mapname))
                {
                    var mapindex = _filenamelist.FindIndex(x => x.Contains(mapname));
                    if (ImageReady != null)
                        ImageReady("LocalGenerator",GetmapByIndex(mapindex));
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }
        private Bitmap GetmapByIndex(int index)
        {
            Bitmap map = null;
            if (_filenamelist != null && _filenamelist.Count > index)
            {
                map = new Bitmap(_filenamelist[index]);
            }
            return map;
        }
        public bool AquireBitmap(int mapindex)
        {
            bool flag = false;
            try
            {
                if (_filenamelist.Count > mapindex)
                {
                    if (ImageReady != null)
                        ImageReady("LocalGenerator",GetmapByIndex(mapindex));
                    flag = true;
                }
            }
            catch
            {

            }
            return flag;
        }
        private List<string> GetExistProduct()
        {
            if (Directory.Exists(_folderpath))
            {
                string[] productList = Directory.GetDirectories(_folderpath);
                List<string> ls = new List<string>();
                foreach (string pa in productList)
                {
                    ls.Add(Path.GetFileName(pa));
                }
                return ls;
            }
            else
            {
                return null;
            }
        }
    }
}
